Dnsmasq   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 36
dl 0
loc 53
rs 10
c 0
b 0
f 0
wmc 5

4 Functions

Rating   Name   Duplication   Size   Complexity  
A setDomain 0 2 1
A addDomainResolver 0 10 1
A appendCustomConfig 0 5 2
A configure 0 8 1
1
import execa from 'execa'
2
import * as fs from 'fs'
3
import OS from '../client/OS'
4
import {Config} from '../models/config'
5
import {getConfig, jaleHomeDir} from '../utils/jale'
6
import {requireSudo} from '../utils/sudo'
7
import Service from './service'
8
9
class Dnsmasq extends Service {
10
    service = 'dnsmasq'
11
    requireRoot = true
12
13
14
    resolverPath = '/etc/resolver'
15
    configPath = `${OS.getInstance().usrLocalDir}/etc/dnsmasq.conf`
16
17
    customConfigPath = `${jaleHomeDir}/dnsmasq.conf`
18
19
    configure = async (): Promise<boolean> => {
20
        const config: Config = await getConfig()
21
22
        this.appendCustomConfig()
23
        this.setDomain(config.tld)
24
        await this.addDomainResolver(config.tld)
25
26
        return true
27
    }
28
29
    /**
30
     * Append our custom configuration file to the dnsmasq.conf.
31
     */
32
    appendCustomConfig = (): void => {
33
        const config = fs.readFileSync(this.configPath, 'utf-8')
34
35
        if (!config.includes(this.customConfigPath))
36
            fs.appendFileSync(this.configPath, `\nconf-file=${this.customConfigPath}\n`)
37
    }
38
39
    /**
40
     * Set our custom tld in our custom dnsmasq config file.
41
     * @param tld
42
     */
43
    setDomain = (tld: string): void => {
44
        return fs.writeFileSync(this.customConfigPath, `address=/.${tld}/127.0.0.1\n`)
45
    }
46
47
    /**
48
     * Create the Resolver config to resolve our custom domain.
49
     * @param tld
50
     */
51
    addDomainResolver = async (tld: string): Promise<boolean> => {
52
        // TODO: Should improve this part, we're executing plain commands in order to bypass issues with root permissions.
53
        await requireSudo()
54
        await execa('sudo', ['mkdir', '-p', this.resolverPath], {shell: true, stdio: 'inherit'})
55
        await execa('sudo', ['bash', '-c', `'echo "nameserver 127.0.0.1" > ${this.resolverPath}/${tld}'`], {
56
            shell: true,
57
            stdio: 'inherit'
58
        })
59
60
        return true
61
    }
62
63
}
64
65
export default Dnsmasq
66